package com.hwj.controller;

import cn.hutool.core.lang.func.Func1;
import cn.hutool.jwt.JWTUtil;
import com.hwj.entity.Comment;
import com.hwj.entity.Post;
import com.hwj.service.CommentService;
import com.hwj.service.PostService;
import com.hwj.utils.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    PostService postService;

    /*
     * 用户添加一条评论
     * */
    @PostMapping("/userAddComment")
    public JsonResponse userAddComment(@RequestBody Comment comment, HttpServletRequest request) {
        String token = request.getHeader("token");
        Object userId = JWTUtil.parseToken(token).getPayload("userId");
        //设置评论的人
        comment.setUserId(Long.parseLong(userId + ""));
        //插入
        commentService.insertSelective(comment);
        //设置一级评论根节点，之后的回复均自带rootid
        if(comment.getCommentRootId()==null){
            comment.setCommentRootId(comment.getCommentId());
            //更新
            commentService.updateByPrimaryKeySelective(comment);
        }

        Post post = postService.selectByPrimaryKey(comment.getPostId());
        Long count = post.getComments();
        count++;
        post.setComments(count);
        postService.updateByPrimaryKey(post);

        return JsonResponse.successResponse(0, "添加评论成功", null);
    }

    /*
    * 查询所有的评论
    * */
    @GetMapping("/selectForUser/{postId}")
    public JsonResponse selectForUser(@PathVariable Long postId) {
        List<Comment> comments = commentService.selectForUser(postId);//一级评论
        for (Comment comment : comments) {  // 查询回复列表
            Comment param = new Comment();
            param.setCommentRootId(comment.getCommentId());
            param.setPostId(postId);
            List<Comment> childrens = commentService.selectForChildList(param);
            childrens = childrens.stream()
                    .filter(child -> !child.getCommentId().equals(comment.getCommentId()))
                    .collect(Collectors.toList());  // 排除当前查询结果里最外层节点
            comment.setChildren(childrens);
        }
        return JsonResponse.successResponse(0, "查询当前帖子所有评论成功",comments);
    }

    /*
    * 前台查询该帖子评论数量
    * */
    @GetMapping("/selectCount/{postId}")
    public JsonResponse selectCount(@PathVariable Long postId) {
        Long count = commentService.selectCount(postId);
        return JsonResponse.successResponse(0, "查询当前帖子所有评论成功",count);
    }

    /*前台删除评论*/
    @DeleteMapping("/delete/{commentId}")
    public JsonResponse delete(@PathVariable Long commentId) {
        int i = commentService.deleteByPrimaryKey(commentId);

        if(i!=0)
            return JsonResponse.successResponse(0, "删除当前评论以及子评论成功",null);
        else
            return JsonResponse.failResponse(-2, "删除失败",null);
    }
}
